package com.xqq.utils;


import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ThreadLocalRandom;


class Device {
    private String deviceName;
    private String position;

    public Device(String deviceName, String position) {
        this.deviceName = deviceName;
        this.position = position;
    }

    public String toJson() {
        return String.format("{\"deviceName\":\"%s\",\"position\":\"%s\"}", deviceName, position);
    }
}

public class TrackGenerator {
    public static void main(String[] args) {
//        String[] names = {
//                "前哨车", "前导车", "后卫车", "应急车",
//                "夏东仑", "杨东卫", "黄磊", "岳扬",
//                "V1-1", "V1-4", "V1-5", "V1-6"
//        };
        String[] names = {
                "前哨车"
        };
        String positionCon = "96.163237,23.637820";
        double baseLongitude = Double.parseDouble(positionCon.split(",")[0]);
        double baseLatitude = Double.parseDouble(positionCon.split(",")[1]);
        double delta = 0.00045; // 大约50米的经纬度变化
        int groupCount = 3; // 组数

        HttpClient httpClient = HttpClient.newHttpClient();

        for (int group = 0; group < groupCount; group++) {
            List<Device> devices = new ArrayList<>();
            try {
                Thread.sleep(5000);
                System.out.println("=================================================================================");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 生成每组的 12 条数据
            for (String name : names) {
                    double randomOffsetLongitude = ThreadLocalRandom.current().nextDouble(-delta, delta);
                    double randomOffsetLatitude = ThreadLocalRandom.current().nextDouble(-delta, delta);
                    double newLongitude = baseLongitude + randomOffsetLongitude;
                    double newLatitude = baseLatitude + randomOffsetLatitude;
                    String position = String.format("%.6f,%.6f", newLongitude, newLatitude);
                    devices.add(new Device(name, position));
            }

            // 发送请求
            List<CompletableFuture<Void>> futures = new ArrayList<>();
            for (Device device : devices) {
                String json = device.toJson();
                CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                    try {
                        HttpRequest request = HttpRequest.newBuilder()
                                .uri(URI.create("http://172.16.1.20:60398/TrackService/addTrack"))
                                .header("Content-Type", "application/json")
                                .POST(HttpRequest.BodyPublishers.ofString(json))
                                .build();
                        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
                        System.out.println("Response: " + response.statusCode() + " for " + json);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                futures.add(future);
            }

            // 等待当前组的所有请求完成
            try {
                CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).get(); // 等待所有请求完成
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
}